import React, { useContext, useState, useEffect } from "react";
import Taro from "@tarojs/taro";
import { CountyDataContextProps } from '../../types/uses/useCoutryDataContext'


const defalut = [];
const CountyDataContext = React.createContext({} as CountyDataContextProps);

export const useCountyDataContext = () => useContext(CountyDataContext);

const GreetProvider: React.FC = props => {
  const [countyData, setCountyData] = useState<any[]>(defalut);
  useEffect(() => {
    Taro.request({
      url: "https://cdn.yryz.com/lovelorn/file/countryData.json",
      success: function(res) {
        setCountyData([...res.data]);
      }
    });
  }, []);

  return (
    <CountyDataContext.Provider value={{ countyData }}>
      {props.children}
    </CountyDataContext.Provider>
  );
};

export default GreetProvider;
